package pseudoAssemblerCompiler.twopassassembler.view.util;

import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;

/**
 * User: Selivasik
 * Date: 01.08.11
 * Time: 19:55
 */
public class WidgetsFactory {
    private static int orientation = Window.getDefaultOrientation();

    public static Composite createComposite(Composite parent, int style) {
        Composite composite = new Composite(parent, style | SWT.DOUBLE_BUFFERED);
        composite.setBackgroundMode(SWT.INHERIT_NONE);
        return composite;
    }

    public static Text createText(Composite composite, int style) {
        Text text = new Text(composite, style);
        return text;
    }

    public static Label createLabel(Composite parent, int style) {
        Label label = new Label(parent, SWT.TRANSPARENT);
//        label.setBackground(label.getDisplay().getSystemColor(SWT.COLOR_BLACK));
        //.setBackgroundMode(SWT.INHERIT_DEFAULT);

        return label;
    }

    public static CheckboxTableViewer createCheckboxTableViewer(Composite parent, int style) {
        CheckboxTableViewer checkboxTableViewer = new CheckboxTableViewer(new Table(parent, style));
        return checkboxTableViewer;
    }

    public static TableViewer createTableViewer(Composite parent, int style) {
        TableViewer tableViewer = new TableViewer(new Table(parent, style));
        return tableViewer;
    }

    public static ComboViewer createComboViewer(Composite parent, int style) {
        ComboViewer comboViewer = new ComboViewer(parent, style);
        return comboViewer;
    }

    public static SmartCombo createSmartCombo(Composite parent, int style) {
        return new SmartCombo(parent, SWT.NONE);
    }

    public static void addPrompt(final Text text, final String defaultText) {
        text.addFocusListener(new FocusListener() {

            public void focusGained(FocusEvent e) {
                if (text.getText().equals(defaultText)) {
                    text.setText("");
                    text.setForeground(null);
                }
            }

            public void focusLost(FocusEvent e) {
                if (text.getText().equals("")) {
                    text.setText(defaultText);
                    text.setForeground(text.getDisplay().getSystemColor(SWT.COLOR_GRAY));
                }
            }
        });
        text.setFocus();
    }

}
